# -*- coding: utf-8 -*-
from models import *
from wtforms import Form, TextField, TextAreaField, BooleanField, DateTimeField, validators
from wtforms.ext.sqlalchemy.fields import QuerySelectField

def _types():
        return ActivityType.query.all()
    
class ActivityForm(Form):
    title = TextField(u'标题', [validators.Length(max=50), validators.Required()])
    activity_type = QuerySelectField(label=u'类型', query_factory=_types, allow_blank=False)
    description = TextAreaField(u'描述')
    start_time = DateTimeField(u'开始时间', format='%Y-%m-%d %H:%M')
    end_time = DateTimeField(u'结束时间', format='%Y-%m-%d %H:%M')
    activity_place = TextField(u'地点', [validators.Required()])
    #invites = relationship('ActivityInvite', backref='activity')
    #公开性
    is_public = BooleanField(u'是否公开', default=True)
    #申请是否需要审核
    need_approve = BooleanField(u'申请是否需要审核', default=False)
    #是否允许活动成员邀请ta的朋友
    allow_invitee_invite = BooleanField(u'是否允许活动成员邀请TA的朋友', default=True)
    
    
    def create_activity(self, creator, invite_users):        
        activity = Activity()
        activity.creator = creator
        activity.title = self.title.data
        activity.activity_type = self.activity_type.data
        activity.description = self.description.data
        activity.start_time = self.start_time.data
        activity.end_time = self.end_time.data
        activity.activity_place = self.activity_place.data
        activity.is_public = self.is_public.data
        activity.need_approve = self.need_approve.data
        activity.allow_invitee_invite = self.allow_invitee_invite.data
        activity.add_invitee(creator, u'参加')
        activity.add_invitees(invite_users, u'未处理')
        activity.save()
        return activity
